<template>
  <basic-container>
    <div class="container-body">
      <el-form :inline="true" :model="query" class="demo-form-inline">
        <el-form-item label="任务名称">
          <el-input size="small" v-model="query.name" placeholder="任务名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="onSearch" :loading="loading">搜索
          </el-button>
          <el-button size="small" icon="el-icon-delete" @click="searchReset" :loading="loading">清空</el-button>
        </el-form-item>
      </el-form>
      <el-table header-row-class-name="container-table-header" :data="data" size="small" style="width: 100%;margin-bottom: 20px;"
                row-key="id" border stripe v-loading="loading" @cell-click="handleCellClick">
        <el-table-column prop="name" label="任务名称" width="300" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="userName" label="申请人">
        </el-table-column>
        <el-table-column prop="startTime" label="接收时间">
        </el-table-column>
        <el-table-column prop="endTime" label="完成时间">
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status | approveStatusTag">{{ scope.row.status | approveStatus }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="comment" label="备注">
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="text" size="small" icon="el-icon-view" @click="showTaskAuditTaskDialog(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="container-page">
        <el-pagination
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="page.current"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </div>
    </div>

    <TaskAuditDialog :dialog="taskAuditDialog" @setValue="setTaskAuditTaskDialog" @closeDialog="closeTaskAuditTaskDialog" ref="taskAuditDialog" />
  </basic-container>
</template>

<script>
import {mapGetters} from "vuex";
import {doneTaskList} from "@/api/act/task";
import TaskAuditDialog from "@/components/act/TaskAuditDialog";

export default {
  directives: {
  },
  components: {
    TaskAuditDialog,
  },
  dictTypes: ['audit_type'],
  data() {
    return {
      importBox: false,
      isEdit: false,
      isView: false,
      viewBox: false,
      formBox: false,
      formTitle: '',
      loading: false,
      query: {},
      form: {
      },
      page: {
        size: 10,
        current: 1,
        total: 0
      },
      data: [],
      selectionRow: [],
      Authorization: '',
      fileList: [], // 文件

      transferDialog: false,
      transferDialogType: '',
      transferDialogArr: [],
      transferDialogMultiple: false,

      taskAuditDialog: false,
    }
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  created() {
    this.onLoad(this.page);
    this.Authorization = 'Bearer ' + this.$store.getters.access_token
  },
  methods: {
    handleCellClick(row, column) {
      if (row && column.label !== "操作"){
        this.showTaskAuditTaskDialog(row)
      }
    },
    submit() {
      this.$refs['optForm'].validate((valid) => {
        if (valid) {
          if(this.form.auditDates) {
            this.form.auditStartDate = this.form.auditDates[0];
            this.form.auditEndDate = this.form.auditDates[1];
          }
          if(this.form.planDates) {
            this.form.planStartDate = this.form.planDates[0];
            this.form.planEndDate = this.form.planDates[1];
          }
          this.fileList && this.fileList.map((file) => {
            this.form.fileUrl = file.response ? file.response.data.url : file.url
          });
          this.loading = true;
          submit(this.form).then(() => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.resetForm();
            this.onLoad(this.page);
          }, () => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },
    showViewBox(row) {
      getObj(row.planId).then((res) => {
        this.form = res.data.data;
        this.viewBox = true;
        this.isView = true;

      });
    },
    handleDelete(row) {
      this.$confirm("确定将选择的数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return delObj(row.renterId);
      })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    resetForm() {
      this.formBox = false;
      this.viewBox = false;
      this.isEdit = false;
      this.isView = false;
      this.form = {};
      this.formTitle = '';
      this.$refs['optForm'] && this.$refs['optForm'].resetFields();
      this.viewBox = false;
      this.printBox = false;
    },
    onSearch() {
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.current = 1;
      this.onLoad(this.page);
    },
    currentChange(current) {
      this.page.current = current;
      this.onLoad(this.page);
    },
    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      if(this.query.publishDates) {
        this.query.startDate = this.query.publishDates[0];
        this.query.endDate = this.query.publishDates[1];
      }
      doneTaskList(Object.assign(params, this.query, page)).then(res => {
        this.loading = false;
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      }, () => {
        this.loading = false;
      });
    },
    handleRemove(file, fileList) {
      this.fileList = fileList
    },
    handleSuccess(response, file, fileList) {
      this.fileList = fileList;
    },
    handlePreview(file) {
      window.open(file.url ? file.url : file.response.data.url)
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    showTaskAuditTaskDialog(row) {
     this.taskAuditDialog = true;
     this.$refs['taskAuditDialog'].initData({processId: row.processId, isView: true});
    },
    closeTaskAuditTaskDialog() {
      this.taskAuditDialog = false;
    },
    setTaskAuditTaskDialog() {
      this.taskAuditDialog = false;
      this.onLoad(this.page);
    },
  }
};
</script>

<style>
</style>
